package com.easymall.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Properties;

public class BaseFactory {

    private static Properties prop = null;

    private static class BaseFactoryInstance {
        private static final BaseFactory INSTANCE = new BaseFactory();
    }

    static {
        URL url = BaseFactory.class.getClassLoader().getResource("conf.properties");
        String path = url.getPath();
        System.out.println(url);
        System.out.println(path);
        System.out.println(path);
        prop = new Properties();
        try {
            prop.load(new FileInputStream(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private BaseFactory() {

    }

    public static BaseFactory getFactory(){
        return BaseFactoryInstance.INSTANCE;
    }

    public <T>T getInstance(Class t) throws Exception {
        String key = t.getSimpleName();
        String className = prop.getProperty(key);
        Class clazz = Class.forName(className);
        T obj = (T) clazz.newInstance();
        Method method = clazz.getDeclaredMethod("init");
        method.invoke(obj);
        return obj;
    }

}
